<template>
  <div class="app-container glories-container">
    <!-- 选择 -->
    <div class="filter-container">
      <el-input v-model="paging.keywords" placeholder="请输入搜索内容" style="width: 200px;" class="filter-item" @keyup.enter.native="getNickname" />
      <el-button v-waves class="filter-item" type="primary" icon="el-icon-search" @click="getNickname">搜索</el-button>
      <el-button v-waves class="filter-item" type="primary" @click="add('称号新增')">新增</el-button>
    </div>

    <!--表格-->
    <el-table
      v-loading="listLoading"
      :data="list"
      border
      fit
      highlight-current-row
      style="width: 100%;"
    >
      <el-table-column label="称号" min-width="100px" align="center">
        <template slot-scope="scope">
          <el-tag type="danger">{{ scope.row.nickname }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="经验" min-width="100px" align="center">
        <template slot-scope="scope">
          <el-tag type="success">{{ scope.row.less }} ~ {{ scope.row.greater }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" min-width="100px" align="center">
        <template slot-scope="scope">
          <el-button type="primary" size="mini" @click="editor(scope.row,'编辑称号')">编辑</el-button>
          <el-button type="danger" size="mini" @click="del(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="paging.count>0" :total="paging.count" :page.sync="paging.page" :limit.sync="paging.limit" @pagination="getNickname" />

    <!-- 弹框 -->
    <el-dialog
      v-loading="listLoading"
      :title="alert.title"
      :visible.sync="alert.centerDialogVisible"
      :before-close="close"
      width="30%"
    >
      <div class="input">
        <el-input v-model="fromData.nickname" placeholder="请输入">
          <template slot="prepend">称号：</template>
        </el-input>
      </div>
      <div class="input">
        <div class="el-date-editor el-range-editor el-input__inner el-date-editor--timerange">
          <i class="el-input__icon el-range__icon el-icon-edit" />
          <input id="" v-model="fromData.less" placeholder="最小经验值" name="" class="el-range-input" type="number">
          <span class="el-range-separator">至</span>
          <input id="" v-model="fromData.greater" placeholder="最大经验值" name="" class="el-range-input" type="number">
        </div>

      </div>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="sureDialogVisible">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import waves from '@/directive/waves'
import { nickname, editorNickname, addNickname, delNickname } from '@/api/set'
import Pagination from '@/components/Pagination'

export default {
  components: { Pagination },
  directives: { waves },
  data() {
    return {
      listLoading: false,
      list: null,
      paging: {
        count: 0,
        page: 1,
        limit: 10,
        keywords: undefined
      },
      alert: {
        title: '测试',
        centerDialogVisible: false
      },
      fromData: {
        nickname: '',
        greater: '',
        less: '',
        id: ''
      }
    }
  },
  created() {
    this.getNickname()
  },
  methods: {
    // 获取表格数据
    getNickname() {
      this.listLoading = true
      nickname(this.paging).then(res => {
        const { data: { count, rows }} = res.data
        this.paging.count = count
        this.list = rows
        this.listLoading = false
      }).catch(() => {
        this.listLoading = false
      })
    },
    editor(resp, type) {
      this.alert.title = type
      this.alert.centerDialogVisible = true
      this.fromData.nickname = resp.nickname
      this.fromData.greater = resp.greater
      this.fromData.less = resp.less
      this.fromData.id = resp.id
    },
    add(type) {
      this.alert.title = type
      this.alert.centerDialogVisible = true
    },
    del(id) {
      this.$confirm('是否删除?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        delNickname(id).then(res => {
          const { msg } = res.data
          this.$message({
            type: 'success',
            message: msg
          })
          this.getNickname()
        }).catch(() => {})
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },

    sureDialogVisible() {
      if (this.alert.title === '编辑称号') {
        this.listLoading = true
        editorNickname(this.fromData.id, this.fromData).then(res => {
          const { msg } = res.data
          // this.$message.success(msg);
          this.$message({
            type: 'success',
            message: msg
          })

          this.listLoading = false
          this.alert.centerDialogVisible = false
          this.getNickname()
          this.close()
        }).catch(() => {
          this.listLoading = false
          this.close()
        })
      } else {
        // 新增
        if (this.fromData.nickname.length === 0) {
          this.$message.warning('称号输入不能为空哦！')
          return
        }
        this.listLoading = true
        addNickname(this.fromData).then(res => {
          const { msg } = res.data
          // this.$message.success(msg);

          this.$message({
            type: 'success',
            message: msg
          })

          this.listLoading = false
          this.alert.centerDialogVisible = false
          this.getNickname()
          this.close()
        }).catch(() => {
          this.listLoading = false
          this.close()
        })
      }
    },
    close() {
      this.alert.centerDialogVisible = false
      this.fromData.nickname = ''
      this.fromData.greater = ''
      this.fromData.less = ''
    }
  }
}
</script>

<style >
.glories-container .input {
    margin-top: 20px;
  }

.glories-container  .el-date-editor--daterange.el-input,
.glories-container .el-date-editor--daterange.el-input__inner,
.glories-container .el-date-editor--timerange.el-input,
.glories-container .el-date-editor--timerange.el-input__inner{
      width:100%;
  }
</style>
